package cn.yuan.xiaoyu.chatmodule.presenter;

import android.text.TextUtils;

import cn.yuan.xiaoyu.chatmodule.activity.ChatActivity;
import cn.yuan.xiaoyu.chatmodule.bean.SendMessageBean;
import cn.yuan.xiaoyu.commonmodule.manager.HttpManager;
import cn.yuan.xiaoyu.commonmodule.net.ResponseListener;
import cn.yuan.xiaoyu.commonmodule.utils.PhoneUtils;
import cn.yuan.xiaoyu.commonmodule.utils.T;

/**
 * Created by yukuoyuan on 2017/2/27.
 * 这是一个聊天界面的逻辑层
 */

public class ChatPresenter {
    private IChatView iChatView;
    private ChatActivity chatActivity;

    public ChatPresenter(IChatView iChatView, ChatActivity chatActivity) {
        this.iChatView = iChatView;
        this.chatActivity = chatActivity;
    }

    /**
     * 发送消息并获得消息
     */
    public void sendMessage(String msg) {
        if (TextUtils.isEmpty(msg)) {
            T.showLong(chatActivity, "请输入内容");
            return;
        } else {
            SendMessageBean sendMessageBean = new SendMessageBean();
            sendMessageBean.result = new SendMessageBean.ResultBean();
            sendMessageBean.result.text = msg;
            sendMessageBean.result.type = 1;
            iChatView.addMsg2List(sendMessageBean);
            iChatView.clearEditText();
        }
        /**
         * 获取手机的设备id
         */
        String uid = PhoneUtils.getInstance(chatActivity).getDeviceId();
        HttpManager.getInstance().sendMsg(uid, msg, new ResponseListener<SendMessageBean>() {
            @Override
            public void onSuccess(SendMessageBean sendMessageBean) {
                iChatView.addMsg2List(sendMessageBean);

            }

            @Override
            public void onFailure(String responseresult) {

            }
        });
    }
}
